/******************************************************************************
* decompose place vs person compenent at state level
* then, project state level fixed effects on covariates
*****************************************************************************/



*regression specification:
global runReg " i.rel_q_prime i.asofy i.asofm i.age  [aw=numobs] "


*in addition to mover variables, also include filing for any bankruptcy
global FEMvVars $keyMvVars bkrty3


*****************************************************************************
** place vs person state level decomposition
*****************************************************************************

*load cut version to get varlist

if $fe_st_estimate==1{
	use "${dataRoot}/tu_mover_delta${ext}_cut.dta", clear


	*mover data (load one variable at a time)
	foreach var of varlist $FEMvVars{
		use num_mv - numobs `var' avg_`var' delta_`var' asofy - rank rel_q - max_rel_q cz ${Geo} using "${dataRoot}/tu_mover_delta${ext}.dta", clear
		
		
		cleanSample2WayFE
				
		
		
		*merge with cz - state crosswalk
		merge m:1 cz using "${proj}/Data/Crosswalk/cz_name"
		keep if _merge==3
		drop _merge
		rename statefip state
		
		labelVars 0 0
		
		**********************************************************
		// run regression to estimate place fixed effect at state level
		**********************************************************
		*** for each vars, run two regression, one with variation FE, one without
		forval i=1/2{
			preserve
				
				*FGW equation(2)
				if `i'==1{
					qui areg `var' i.state ${runReg}, absorb(varID)
				}
				else{
					qui reg  `var' i.state ${runReg}	
				}
				 
				*Get coefficients on place FE
				parmest, norestore 
				
				qui gen state = regexs(1) if regexm(parm, "(^[0-9]+)b?\.state")
				qui keep if state != "" 
				destring state, replace
				
				qui gen beta = estimate 
				qui gen se = stderr
				qui gen upper = beta + 1.96*stderr
				qui gen lower = beta - 1.96*stderr

				* save place FE
				drop estimate stderr dof t p min95 max95
				compress
				if `i'==1{
					save "${dataRoot}/TU_place_estimates_`var'.dta", replace
				}
				else{
					save "${dataRoot}/TU_place_avg_`var'.dta", replace		
				}			
				
			restore	
		}
	}

}

*****************************************************************************
** post estimation, merge all variables together
*****************************************************************************


use move_t rel_q rel_q_prime num_mv cz numobs  using "${dataRoot}/tu_mover_delta${ext}.dta"
cleanSample2WayFE
tab num_mv [w=numobs]

if $fe_st_merge_est==1{
	*** collapse data to cz level and merge with estimated place FE
	*	actually keyMvVars are no longer used. Keep to get their labels
	use cz numobs  using "${dataRoot}/tu_mover_delta${ext}.dta", clear
	
	collapse (sum) numobs, by (cz) fast


	*merge with dummy data to get varlist keyMvVars
	merge 1:1 cz using  "${dataRoot}/tu_mover_delta${ext}_cut.dta", keepusing($FEMvVars )
	drop _merge

	*merge with cz - state crosswalk
	merge m:1 cz using "${proj}/Data/Crosswalk/cz_name"
	keep if _merge==3
	drop _merge
	rename statefip state
		
	tab state

	*collpase at state level
	gen n=1
	collapse $FEMvVars (sum)n [fw=numobs], by(state) 
	rename n numobs


	** we got the correct number of observations in each state and now look up FEs
	*merge estimated place FE
	foreach var of varlist $FEMvVars{
		*place effect
		merge 1:1 state using "${dataRoot}/TU_place_estimates_`var'.dta" 
		qui tab _merge
		qui keep if _merge==3
		drop _merge
		rename beta `var'_p
		
		*raw place average conditional on time varying parameters
		merge 1:1 state using "${dataRoot}/TU_place_avg_`var'.dta" 
		qui tab _merge
		qui keep if _merge==3
		drop _merge
		rename beta `var'_fe
	}

	*label vars, (no upper case for first letter, short name)
	labelVars 0 0
		
	*customer effect is place diff - place FE
	foreach var of varlist $FEMvVars{
		gen `var'_c = `var'_fe -`var'_p
	}


	*save place vs customer decomposition data
	compress
	save "$dataRoot/TU_place_vs_customer_st${ext}_v1.dta", replace

}


*****************************************************************************
** aggregate data for projection of place effects usig LASSO in R
*****************************************************************************


*load covariates
use "${dataRoot}/tu_acs_cz_${sumStatsyr}", clear
drop state
rename statefip state
gen job_employed = 100 - job_unemployed


*collapse at state level
collapse $proj_covar  [fw=numobs], by(state) 

*merge with state level TU place effects
merge 1:1 state using "$dataRoot/TU_place_vs_customer_st${ext}_v1.dta"
drop _merge
	
rename state statefip
merge 1:1 statefip using "${dataRoot}/st_covariates", assert(3) nogen
	
*** standardize LHS/RHS variables
foreach var of varlist $proj_covar{		
	qui drop if `var'==.
}
foreach var of varlist $proj_covar{		
	*standardize variable
	qui sum `var'
	qui replace `var' = (`var'-`r(mean)')/`r(sd)'
}

foreach var of varlist $FEMvVars{		
	*standardize variable
	qui sum `var'_fe
	qui replace `var'_fe = (`var'_fe-`r(mean)')/`r(sd)'
	qui sum `var'_p
	qui replace `var'_p  = (`var'_p -`r(mean)')/`r(sd)'
}

** save place-person decomposition and covariates
keep $FEMvVars *_fe *_p numobs $proj_covar
save "$dataRoot/TU_place_vs_customer_cov_std.dta", replace



